/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.model;

import java.applet.Applet;
import java.io.File;
import javax.swing.JFileChooser;
import netscape.javascript.JSObject;
import offset.nodes.client.dialog.io.view.ExportBookPanel;
import offset.nodes.client.dialog.navigation.view.AbstractCardPanel;
import offset.nodes.client.dialog.navigation.view.RepositoryPanel;
import offset.nodes.client.model.Server;

/**
 *
 * @author Walter Lütgenau
 */
public class RepositoryModel extends NavigationModel implements NodeSelectionListener, ServiceClient, RepositoryAware {

    public static final String ID_PATH = "navigation:repository:path";
    Applet applet;
    RepositoryPanel panel;
    Server server;
    String repository;

    @Override
    public void initPanel() throws Exception {
        this.panel = new RepositoryPanel();
        this.panel.setNodeSelectionListener(this);
        if (server != null)
            this.panel.setServer(server);
    }

    @Override
    public AbstractCardPanel getPanel() {
        return panel;
    }

    @Override
    public void setApplet(Applet applet) {
        this.applet = applet;
    }

    @Override
    public void setServer(Server server) {
        this.server = server;
        if (panel != null)
            panel.setServer(server);
    }

    @Override
    public void setRepository(String repository) {
        this.repository = repository;
    }

    /**
     * Set the location of the content frame.
     * 
     * @param location the location
     */
    @Override
    public void nodeSelected(String path) {
        System.out.println(applet);
        JSNode window = new JSNode((JSObject) JSObject.getWindow(applet));
        JSNode contentFrame = window.getNode("parent.contentFrame");
        contentFrame.getObject().setMember("location", repository + path);
    }
    
    /**
     * Extract the path and set it into the tree.
     * 
     * @throws Exception an exception
     */
    @Override
    public void updatePage() throws Exception {
        String path = getPath();
        if (path != null)
            panel.addPath(path);
    }

    /**
     * Find the path information in the content frame and return it.
     * The path is encoded into a hidden field.
     * 
     * @return the path
     */
    protected String getPath() {
        JSNode window = new JSNode((JSObject) JSObject.getWindow(applet));
        JSDocument document = new JSDocument(window.getNode("parent.contentFrame.document"));
        JSNode pathNode = document.getElementById(ID_PATH);
        if (pathNode == null)
            return null;

        return (String) pathNode.getAttributes().getAttribute(JSNode.ATT_VALUE);
    }
}
